﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;


public partial class calendar : System.Web.UI.Page
{
    private DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["year"] != null && Request.QueryString["month"] != null)
                calendar.VisibleDate = DateTime.Parse(Request.QueryString["year"].ToString() + "-" + Request.QueryString["month"].ToString() + "-01");
            else
                calendar.VisibleDate = DateTime.Today;

            DateTime prevMonth = calendar.VisibleDate.AddMonths(-1);
            DateTime nextMonth = calendar.VisibleDate.AddMonths(1);
            calendar.PrevMonthText = "<a class=np href=\"calendar.aspx?year=" + prevMonth.Year + "&month=" + prevMonth.Month + "\"><<上个月</a>";
            calendar.NextMonthText = "<a class=np href=\"calendar.aspx?year=" + nextMonth.Year + "&month=" + nextMonth.Month + "\">下个月>></a>";
            BindData();
        }
    }
    private void BindData()
    {
        DateTime firstDate = new DateTime(calendar.VisibleDate.Year, calendar.VisibleDate.Month, 1);
        DateTime lastDate = firstDate.AddMonths(1);

        FillData(firstDate, lastDate);
    }
    protected void FillData(DateTime startTime, DateTime endTime)
    {
        dt.Columns.Add("event_title");
        dt.Columns.Add("event_content");
        dt.Columns.Add("publish_date");
        SqlConnection sc = new SqlConnection("Data Source=localhost;Initial Catalog=Project;Integrated Security=True");
        //TO-DO:修改连接字符串
        sc.Open();
        SqlCommand get = new SqlCommand("select EVENT_TITLE,EVENT_CONTENT,PUBLISHDATE from INFO_CAL,(select PUBLISHDATE as PDate,MAX(EVENT_ID) as PID from INFO_CAL group by PUBLISHDATE) t1 where t1.PID=INFO_CAL.EVENT_ID and PUBLISHDATE>=@start and PUBLISHDATE<@end", sc);
        //TO-DO:建立数据表
        get.Parameters.AddWithValue("@start", startTime);
        get.Parameters.AddWithValue("@end", endTime);
        SqlDataReader r = get.ExecuteReader();
        while (r.Read())
        {
            DataRow row = dt.NewRow();
            row["event_title"] = r.GetString(r.GetOrdinal("EVENT_TITLE"));
            row["event_content"] = r.GetString(r.GetOrdinal("EVENT_CONTENT"));
            row["publish_date"] = r.GetDateTime(r.GetOrdinal("PUBLISHDATE"));
            dt.Rows.Add(row);
        }
        r.Close();
    }
    protected void calendar_DayRender(object sender, DayRenderEventArgs e)
    {
        Random rand = new Random();

        if (e.Day.IsOtherMonth)
        {
            e.Cell.Controls.Clear();
        }
        else
        {
            e.Cell.BorderWidth = 1;
            e.Cell.CssClass = "FullDay";

            DateTime nextDate;
            Literal ltl = new Literal();
            ltl.Text = "";
            ltl.Text += "<DIV class=\"BlackDate\">" + e.Day.Date.Day + "</DIV>";
            ltl.Text += e.Day.Date == calendar.TodaysDate ? "<DIV class=\"RedDate\">" + e.Day.Date.Day + "</DIV>" : "<DIV class=\"WhiteDate\">" + e.Day.Date.Day + "</DIV>";
            ltl.Text += "<br/><a class=\"date\" href=\"\">添加事件</a>";
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow row = dt.Rows[i];
                    nextDate = Convert.ToDateTime(row["publish_date"].ToString());

                    if (nextDate == e.Day.Date)
                    {
                        e.Cell.CssClass = "NotDay";


                        string content = row["event_title"].ToString() + "<br/>" + row["event_content"].ToString();
                        ltl.Text += "<a class=\"date\" href=\"\">查看更多事件</a>";
                        ltl.Text += "<div>" + content + "</div>";
                    }
                }
            }

            e.Cell.Controls.Add(ltl);
        }
    }
}